import {Page, Pageable, Sort} from "../utils/Page";
import fetcher from "../apis/Fetcher";
import {NumberIdAndName} from "../types/BasicModels";
import {ResponseType} from "@zxy-cn/fetcher";
import {OffsetDateTime} from "@js-joda/core";
import {TablePaginationConfig} from "antd";
import {SorterResult} from "antd/es/table/interface";

export interface AddGoodsRequest {
    name: String
    amount: number
    price: number
    barcode: string
    factoryId?: number
    categoryId?: number
    brandId?: number
    boxSpecification?: string
    costPrice?: number
    units: EditGoodsUnitRequest[]
    registrationCertificateNumber?: string
    recordFilingNumber?: string
    expressionPeriod?: string
    note?: string
    executiveStandard?: string
    subGoodsList: Array<EditSubGoodsRequest>
}

export interface EditSubGoodsRequest{
    id?: number
    name: string
    amount: number
    number: string
}

export interface EditGoodsUnitRequest {
    name: string
    basicMultiple: number
    id?: number
    updatable: boolean
}

export function addGoods(addGoodsRequest: AddGoodsRequest) {
    return fetcher.post("goods", {}, addGoodsRequest)
}

export interface QueryGoodsRequest {
    name?: string
    barcode?: string
    categoryIds?: number[]
    brandIds?: number[]
    factoryIds?: number[]
    nameOrBarcode?:string
    editMarketOrderId?: number
    editMarketReturnOrderId?: number
    editMarketSaleDocumentId?: number
    disable?:boolean
}

export function queryGoods(queryGoodsRequest: QueryGoodsRequest, pageable: Pageable) {
    return fetcher.get<Page<QueryGoodsResponse>>("goods", {...queryGoodsRequest, ...pageable.toRequestParams()})
}

export interface QueryGoodsResponse {
    name: string
    amount: number
    price: number
    barcode: string
    createdDateTime: OffsetDateTime
    lastModifiedDateTime: OffsetDateTime
    id: number
    brand?: NumberIdAndName
    category?: NumberIdAndName
    factory?: NumberIdAndName
    boxSpecification?: string
    costPrice?: number
    units: Array<GoodsUnitResponse>
    unit?: NumberIdAndName
    registrationCertificateNumber?: string
    recordFilingNumber?: string
    expressionPeriod?: number
    executiveStandard: string
    compositeGoodsAmount: number
    disable:boolean
}

export interface SubGoodsResponse {
    id: number
    name: string
    amount: number
    number: string
}

export interface GoodsUnitResponse {
    id: number
    name: string
    basicMultiple: number
    updatable: boolean
}

export interface QueryGoodsUnitResponse {
    id: number
    name: string
    basicMultiple: number
}

export interface ExportGoodsToExcelRequest extends QueryGoodsRequest {
    fields?: string[]
}

export function exportGoodsToExcel(queryGoodsRequest: ExportGoodsToExcelRequest, sort?: Sort) {
    return fetcher.get<Blob>("goods/excel", {...queryGoodsRequest, sort: sort?.toRequestParam(),}, {responseType: 3})
}

export function updateGoods(id: number, addGoodsRequest: AddGoodsRequest) {
    return fetcher.put(`goods/${id}`, {}, addGoodsRequest)
}


export function getAllGoodsSummaries(request?: QueryGoodsRequest) {
    return fetcher.get<GoodsSummary[]>("goods", {
        projection: "summary",
        ...request,
    })
}

export function queryGoodsSummariesByPage(pageable:Pageable,request?: QueryGoodsRequest){
    return fetcher.get<Page<GoodsSummary>>("goods", {
        projection: "summary",
        ...request,
        ...pageable.toRequestParams(),
    })
}

export interface GoodsSummary {
    id: number
    name: string
    barcode: string
    subGoodsList: SubGoodsResponse[]
}

export function importGoodsFromExcel(file: File) {
    let formData = new FormData();
    formData.append("file", file)
    return fetcher.post<number>("goods/excel/import", {}, formData, {
        timeout: 30000
    })
}

export function downloadImportTemplate() {
    return fetcher.get<Blob>("goods/excel/import/template", {}, {responseType: 3})
}

export function downloadImportSubGoodsTemplate() {
    return fetcher.get<Blob>("goods/sub/excel/import/template", {}, {responseType: 3})
}

export function importSubGoodsFromExcel(file: File) {
    let formData = new FormData();
    formData.append("file", file)
    return fetcher.post<number>("goods/sub/excel/import", {}, formData, {
        timeout: 30000
    })
}

export function getGoodsDetail(id: number) {
    return fetcher.get<GoodsDetail>(`goods/${id}`)
}

export interface GoodsDetail {
    name: string
    amount: number
    price: number
    costPrice?:number
    barcode: string
    createdDateTime: OffsetDateTime
    lastModifiedDateTime: OffsetDateTime
    id: number
    brand?: NumberIdAndName
    category?: NumberIdAndName
    factory?: NumberIdAndName
    boxSpecification?: string
    lastCheckInventoryRecordItem?: LastCheckInventoryRecordItem
    units: Array<GoodsUnitResponse>
    registrationCertificateNumber?: string
    recordFilingNumber?: string
    expressionPeriod?: string
    executiveStandard?: string
    note?: string
    subGoodsList: SubGoodsResponse[]

}

export interface LastCheckInventoryRecordItem {
    checkInventoryRecordId: number
    amount: number
    createdDateTime: OffsetDateTime
}

export function getAllGoodsWithMarketInStore(marketId: number) {
    return fetcher.get<GoodsWithMarketInStore[]>("goods", {
        marketId
    })
}

export function getAllGoodsWithMarketInStoreInMakret(marketId: number) {
    return fetcher.get<GoodsWithMarketInStore[]>("goods", {
        marketId,
        onlyInMarket: true
    })
}

export interface GoodsWithMarketInStore {
    id: number
    name: string
    amount: number
    barcode: string
    amountInMarket: number
    subGoodsList:SubGoodsResponse[]
}

export function deleteGoods(id: number) {
    return fetcher.delete(`goods/${id}`)
}

export function getAllGoodsUnits() {
    return fetcher.get<string[]>("goods/units")
}

export interface BatchUpdateGoodsRequest {
    goodsIds: number[];
    categoryId?: number;
    brandId?: number;
    boxSpecification?: string;
    expressionPeriod?: string;  // 时间段字符串格式
    amount?: number;
    costPrice?: number;
    price?: number;
}

export function batchUpdateGoods(request:BatchUpdateGoodsRequest){
    return fetcher.put("goods", {},request)
}

export function enableGoods(id:number){
    return fetcher.patch(`goods/${id}/enable`)
}

export function disableGoods(id:number){
    return fetcher.patch(`goods/${id}/disable`)
}

export function batchEnableGoods(goodsIds:number[]){
    return fetcher.patch(`goods/batch/enable`,{
        goodsIds
    })
}

export function batchDisableGoods(goodsIds:number[]){
    return fetcher.patch(`goods/batch/disable`,{
        goodsIds
    })
}